// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Current Tunnel Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 42 : TYPE_CURRENTTUNNEL


// Static Values
public value CurrentTunnel_value33 = 0;

// Tables

event ObjectMain
	temp6 = 0
	temp7 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		CheckLower(object[currentPlayer].xpos, object.value0)
		temp0 = checkResult
		CheckGreater(object[currentPlayer].xpos, object.value1)
		temp0 |= checkResult
		CheckLower(object[currentPlayer].ypos, object.value2)
		temp0 |= checkResult
		CheckGreater(object[currentPlayer].ypos, object.value3)
		temp0 |= checkResult
		if temp0 == 0
			temp7 = 1
			if CurrentTunnel_value33 == 0
				temp6 = 1
				CheckEqual(object[currentPlayer].state, PlayerObject_Hurt)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_Knockback)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_Death)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_Drown)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_Clinging)
				temp0 |= checkResult
				if temp0 == 0
					object[currentPlayer].state = PlayerObject_Clinging
					object[currentPlayer].animation = ANI_FANROTATE
					object[currentPlayer].tileCollisions = 1
				end if
				if object[currentPlayer].animation == ANI_FANROTATE
					if object[currentPlayer].xvel == 0
						temp1 = object.value3
						temp1 -= object.value2
						temp1 >>= 1
						temp1 += object.value2
						if object[currentPlayer].ypos < temp1
							object[currentPlayer].ypos += 0x40000
						else
							object[currentPlayer].ypos -= 0x40000
						end if
					end if
				end if
			else
				CurrentTunnel_value33--
				if object[currentPlayer].state == PlayerObject_Clinging
					object[currentPlayer].state = PlayerObject_HandleAir
					object[currentPlayer].xvel = 0x40000
					object[currentPlayer].speed = 0x40000
				end if
			end if
		else
			if object[currentPlayer].state == PlayerObject_Clinging
				object[currentPlayer].state = PlayerObject_HandleAir
				object[currentPlayer].xvel = 0x40000
				object[currentPlayer].speed = 0x40000
			end if
		end if
	next
	object.priority = temp6
	if object.propertyValue == 0
		object[+1].priority = temp6
	else
		object[-1].priority = temp6
	end if
	if temp7 == 0
		object.value4 = 0
	end if
	if object.value4 != 0
		object.value4--
		if temp7 == 1
			if object.value4 <= 0
				if object.propertyValue == 0
					temp0 = object[+1].value4
				else
					temp0 = object[-1].value4
				end if
				if temp0 == 0
					StopSfx(SfxName[Waterfall])
					PlaySfx(SfxName[Waterfall Loop], 0)
					object.value4 = 31
				end if
			end if
		end if
	else
		if temp7 == 1
			if object.propertyValue == 0
				temp0 = object[+1].value4
			else
				temp0 = object[-1].value4
			end if
			if temp0 == 0
				PlaySfx(SfxName[Waterfall], 0)
				object.value4 = 31
			end if
		end if
	end if
end event


event ObjectStartup
	CurrentTunnel_value33 = 0
	foreach (TypeName[Current Tunnel], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].propertyValue == 0
			arrayPos1 = arrayPos0
			arrayPos1++
			object[arrayPos0].value0 = object[arrayPos0].xpos
			object[arrayPos0].value1 = object[arrayPos1].xpos
			temp1 = object[arrayPos0].ypos
			temp2 = object[arrayPos0].ypos
		else
			arrayPos1 = arrayPos0
			arrayPos1--
			object[arrayPos0].value0 = object[arrayPos1].xpos
			object[arrayPos0].value1 = object[arrayPos0].xpos
			temp1 = object[arrayPos0].ypos
			temp2 = object[arrayPos0].ypos
		end if
		if temp1 < temp2
			object[arrayPos0].value2 = temp1
			object[arrayPos0].value3 = temp2
		else
			object[arrayPos0].value2 = temp2
			object[arrayPos0].value3 = temp1
		end if
		object[arrayPos0].value2 -= 0x600000
		object[arrayPos0].value3 += 0x600000
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
